package cn.com.pansky.action.myaccount;

import java.sql.Timestamp;

import com.alibaba.fastjson.JSONObject;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.action.logs.Log;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.CardNumUtil;

public class UpdPhoneNum_web extends CommonAction {
	private Log log = new Log();
	CommonService cs = new CommonService();
	/**
	 * 给新手机号发送验证码
	 * @throws Exception 
	 */
	public void getCode_web() throws Exception{
		String AA032 = getParam("AA032");
		String findUser = "select * from RC10 where AA032 = ? AND AA003 = '0'";
		Model model = SqlHelper.queryOne(findUser,AA032);
		if (null == model) {
			JSONObject obj = cs.aliyunSms(AA032);
			if (!"0".equals(obj.getString("code"))) {
				JSONObject json = new JSONObject();
				json.put("isPass", false);
				json.put("info", obj.getString("msg"));
				context.getDatas().put("result", json);
				return;
			}
			String code = obj.getString("content");
			context.getServletSession().setAttribute("AA032", AA032);
			context.getServletSession().setAttribute("AF016", code);
			SqlHelper.execute("insert into CA20 values(${~UUID}, ?, ?, ?, ?)", AA032,
					new java.sql.Timestamp(System.currentTimeMillis()),code,new Timestamp(System.currentTimeMillis()).getTime());
			context.getServletSession().setAttribute("AA032_1", AA032);
			
			datas.put("isPass", true);
			datas.put("info", "验证码已发送");
		}else{
			datas.put("isPass", false);
			datas.put("info", "该账号已存在，请更换其他手机号");
		}
		context.getDatas().put("result", datas);
	}
	
	/**
	 * 更新手机号
	 */
	public void updPhone_web(){
		//日志记录start
		String AF001 = log.start(" ", "UP");
		//判断验证码和手机号是否正确
		String AA032_1 = (String) context.getSessionVal("AA032_1");
		
		String AA032 = getParam("AA032");
		String AF016 = getParam("AF016");
		String CA003 = getParam("CA003");
		String CA006 = getParam("CA006");
		CA006 = CardNumUtil.cardNum(CA006);
		if (AA032.equals(AA032_1)) {
			Model m = SqlHelper.queryOne("select * from (select * from CA20 where AA032 = ? and AF016 = ? order by AF014 desc) where rownum = 1", 
					AA032,AF016);
			//当前时间
			long nowTime = new Timestamp(System.currentTimeMillis()).getTime();
			if (m != null) {
				//验证码发送时间
				long AF017 = m.getLong("AF017");
				double time = (nowTime - AF017)/1000/60;
				if (time <= 5) {
					String queryRC03 = "select CA003,CA006 from RC03 where CA001 = (select CA001 from RC10 where AA032 = ? AND AA003 = '0')";
					Model model = SqlHelper.queryOne(queryRC03, context.getSessionVal("AA032"));
					if (CA003.equals(model.getString("CA003")) && CA006.equals(model.getString("CA006"))) {
						String updPhone = "update RC10 set AA032 = ?, AA036 = ? where AA032 = ? AND AA003 = '0'";
						int i = SqlHelper.execute(updPhone, AA032,AA032,context.getSessionVal("AA032"));
						if (i == 1) {
							context.getServletSession().removeAttribute("AA032_1");
							context.getServletSession().removeAttribute("AA032");
							context.getServletSession().removeAttribute("user");
							datas.put("isPass", true);
							datas.put("info", "手机号变更成功，请使用新手机号登陆");
						} else {
							datas.put("isPass", false);
							datas.put("info", "修改失败，请联系管理员");
						}
					} else {
						if (context.getSessionVal("error")!=null) {
							Integer num = (Integer) context.getSessionVal("error");
							if (num == 2) {
								//密码输入错误3次
								String lock = "update RC10 set AA005 = '1', AA034 = ? where AA032 = ? AND AA003 = '0'";
								SqlHelper.execute(lock,new Timestamp(System.currentTimeMillis()).getTime(),context.getSessionVal("AA032"));
								context.getServletSession().removeAttribute("AA032_1");
								context.getServletSession().removeAttribute("AA032");
								context.getServletSession().removeAttribute("user");
								context.getServletSession().removeAttribute("error");
								datas.put("isPass", false);
								datas.put("info", "您的账户已锁，请一天后再尝试修改。");
							} else {
								context.getServletSession().setAttribute("error",(Integer)context.getSessionVal("error")+1);
								datas.put("isPass", false);
								datas.put("info", "您输入的姓名、身份证号与当前账号的信息不符，请核实信息后再操作。");
							}
						} else {
							context.getServletSession().setAttribute("error",1);
							datas.put("isPass", false);
							datas.put("info", "您输入的姓名、身份证号与当前账号的信息不符，请核实信息后再操作。");
						}
					}
				} else {
					datas.put("isPass", false);
					datas.put("info", "验证码已失效，请重新获取");
				}
			} else {
				datas.put("isPass", false);
				datas.put("info", "验证码输入有误，请重新输入");
			}
		} else {
			datas.put("isPass", false);
			datas.put("info", "该手机号未发送验证码，请确认手机号");
		}
		//日志记录end
		boolean result = log.end(AF001, datas.get("isPass"), datas.get("info"));
		System.out.println("日志：修改手机号"+result);
		context.getDatas().put("result", datas);
	}
}
